﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using bts_webapp.Models;

namespace bts_webapp
{
    public partial class StationManagement : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormItem)
            {
                UxStation uxStation = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxStation;
                var stationUpdate = uxStation.GetData();
                var station = this.DbContext.VIBA_TRAMs.FirstOrDefault(u => u.Tram_Id.ToLower() == stationUpdate.Tram_Id.ToLower());
                if (station != null)
                {
                    station.Tram_Id = stationUpdate.Tram_Id;
                    station.Tram_Ten = stationUpdate.Tram_Ten;
                    station.Tram_DiaChi = stationUpdate.Tram_DiaChi;
                    station.NguoiPhuTrach = stationUpdate.NguoiPhuTrach;
                    station.GhiChu = stationUpdate.GhiChu;
                    station.Status = stationUpdate.Status;
                    
                    this.DbContext.SubmitChanges();
                }

            }
        }

        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            
        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            this.RadGrid1.DataSource = this.DbContext.VIBA_TRAMs.ToList();
        }

        protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                var stationId = (e.Item as GridDataItem)["Tram_Id"].Text;
                var station = this.DbContext.VIBA_TRAMs.FirstOrDefault(u => u.Tram_Id.ToLower() == stationId.ToLower());
                if (station != null)
                {
                    this.DbContext.VIBA_TRAMs.DeleteOnSubmit(station);
                    this.DbContext.SubmitChanges();
                }
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            var station = e.Item.DataItem as VIBA_TRAM;

            //insert new station
            if (e.Item is GridEditFormInsertItem)
            {
                UxStation uxStation = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxStation;
                uxStation.EnableFieldCode(true);
                uxStation.InsertOrUpdate = true;
            }
            //update user
            else if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                UxStation uxStation = e.Item.FindControl(GridEditFormItem.EditFormUserControlID) as UxStation;
                uxStation.BindData(station);
                uxStation.EnableFieldCode(false);
                uxStation.InsertOrUpdate = false;
            }

            if (e.Item is GridDataItem)
            {
                var dataItem = e.Item as GridDataItem;
                dataItem["Status"].Text = GetStatus(dataItem["Status"].Text);

            }
        }

        private string GetStatus(string status) {
            return status == "1" ? "<font color='blue'>Hoạt động</font>" : "<font color='red'>Không hoạt động</font>";
        }

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            
        }

        protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
        {
            if (e.Item is GridEditFormInsertItem)
            {
                UxStation uxStation = e.Item.FindControl(GridEditFormInsertItem.EditFormUserControlID) as UxStation;
                var station = uxStation.GetData();
                if (!IsExists(station.Tram_Id))
                {
                    this.DbContext.VIBA_TRAMs.InsertOnSubmit(station);
                    this.DbContext.SubmitChanges();
                    uxStation.SetErrorStatus(string.Empty);
                }
                else
                {
                    uxStation.SetErrorStatus("Mã trạm đã tồn tại, xin chọn mã khác.");
                    e.Canceled = true;
                }
            }
        }

        private bool IsExists(string stationId)
        {
            var context = new BTSModelDataContext();
            var item = context.VIBA_TRAMs.FirstOrDefault(x => x.Tram_Id.ToLower() == stationId.ToLower());
            return item != null;
        }
    }
}